﻿
@{
    ViewBag.Title = "Index";
}

<div class="panel-body" style="padding-bottom:0px;">
    <div class="panel panel-default">
        <div class="panel-heading">查询条件</div>
        <div class="panel-body">
            <form id="formSearch" class="form-horizontal">
                <div class="form-group" style="margin-top:15px">
                    <label class="control-label col-sm-2" for="txtName">客户姓名:</label>
                    <div class="col-sm-3">
                        <input type="text" class="form-control" id="txtName">
                    </div>
                    <div class="col-sm-4" style="text-align:left;">
                        <button type="button" style="margin-left:50px" id="btn_query" class="btn btn-primary" onclick="QueryData()">查询</button>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <div id="toolbar" class="btn-group">
        <input type="hidden" id="index" value="" />
        <button id="btn_add" type="button" class="btn btn-default" data-toggle="modal" data-target="#myModal">
            <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>新增
        </button>
        <button id="btn_edit" type="button" class="btn btn-default" data-toggle="modal" data-target="#myModal">
            <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>修改
        </button>
        <button id="btn_delete" type="button" class="btn btn-default" onclick="Delete()">
            <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>删除
        </button>
    </div>
    <table id="tb1"></table>

</div>
<!-- 模态框（Modal） -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                    ×
                </button>
                <h4 class="modal-title" id="myModalLabel">
                    数据操作
                </h4>
            </div>
            <div class="modal-body">

                <form class="form-horizontal" role="form" id="editId">

                    <input type="hidden" name="Id" id="id" value="" />
                    <input type="hidden" name="TXTid" id="txtid" value="0" />

                    <div class="form-group">
                        <label for="firstname" class="col-sm-2 control-label">姓名</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="Name" name="Name" placeholder="请输入用户姓名">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="firstname" class="col-sm-2 control-label">性别</label>
                        <div class="col-sm-10">
                            <label>
                                <input type="radio" name="Sex0" id="Sex0" value="0" checked>男
                                <input type="radio" name="Sex1" id="Sex1" value="1">女
                            </label>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="firstname" class="col-sm-2 control-label">出生日期</label>
                        <div class="input-group input-medium date-picker input-daterange" data-date-format="yyyy-mm-dd">
                            <input id="BirthDay" name="BirthDay" class="form-control" type="text" value="@Html.Raw(DateTime.Now.ToString("MM-dd-yyyy"))">
                        </div>
                    </div>


                    <div class="form-group">
                        <label for="firstname" class="col-sm-2 control-label">家庭籍贯</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="NativePlace" name="NativePlace" placeholder="请输入家庭省份城市">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="firstname" class="col-sm-2 control-label">现在居住地址</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="Address" name="Address" placeholder="请输入省份城市">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="firstname" class="col-sm-2 control-label">手机号</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="Tel" name="Tel" placeholder="请输入11位手机号">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="firstname" class="col-sm-2 control-label">身份证号</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="cardId" name="cardId" placeholder="请输入18位身份证号">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="firstname" class="col-sm-2 control-label">登录密码</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="Password" name="Password" placeholder="请输入6六位数密码">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="firstname" class="col-sm-2 control-label">邮箱</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="Email" name="Email" placeholder="请输入正确的邮箱地址">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="firstname" class="col-sm-2 control-label">角色</label>
                        <div class="col-sm-5">
                            <select class="form-control m-b" name="account" id="RoleId">
                                @foreach (var item in ViewBag.role)
                                {
                                    <option value="@item.Id">@item.Name</option>
                                }
                            </select>
                        </div>
                    </div>

                </form>

            </div>


            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">
                    关闭
                </button>
                <button type="button" id="btnAdd" class="btn btn-primary">
                    提交更改
                </button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->



@section Scripts{

    <!-- Mainly scripts -->
    <script src="~/Content/Version/js/jquery-3.1.1.min.js"></script>
    <script src="~/Content/Version/js/bootstrap.min.js"></script>
    <!-- Data picker -->
    <script src="~/Content/Version/js/plugins/datapicker/bootstrap-datepicker.js"></script>

    <script src="~/Content/bootstraptable/dist/bootstrap-table.js"></script>
    <link href="~/Content/bootstraptable/dist/bootstrap-table.css" rel="stylesheet" />
    <script src="~/Content/bootstraptable/dist/locale/bootstrap-table-zh-CN.min.js"></script>
    <link href="~/Content/layer/layer/mobile/need/layer.css" rel="stylesheet" />
    <script src="~/Content/layer/layer/layer.js"></script>

    <script>
        $(function () {
            ShowTable();

            //日期控件显示
            var option = {
                language: "zh-CN",
                format: 'yyyy-mm-dd',
                autoclose: true,// 选中之后自动隐藏日期选择框
                clearBtn: true// 清除按钮
            }
            $('#BirthDay').datepicker(option);

        });


        $("#btnAdd").click(function () {
            var edit = $("#editId").serializeArray();
            //alert(edit);
            if ($("#txtid").val() == 0) {
                $.ajax({
                    url: "/lanlord/Add",
                    data: {
                        name: $("#Name").val(),
                        sex: $("#Sex0").val(),
                        sex: $("#Sex1").val(),
                        birthDay: $("#BirthDay").val(),
                        nativePlace: $("#NativePlace").val(),
                        address: $("#Address").val(),
                        tel: $("#Tel").val(),
                        cardId: $("#cardId").val(),
                        password: $("#Password").val(),
                        email: $("#Email").val(),
                        roleId: $("#RoleId").val()
                    },
                    success: function (fun) {

                        if (fun == "Y") {
                            alert("添加成功");
                            QueryData();
                        }
                        else {
                            alert("失败");
                        }
                    }

                })
            } else {
                $.ajax({
                    url: "/lanlord/Edit",
                    data: edit,
                    success: function (res) {
                        if (res == "Y") {
                            alert("修改成功")
                            $("#txtid").val("0");
                            QueryData();
                        } else {
                            alert("修改失败")
                        }
                    }
                })
            }

        });

        //删除
        $("#btn_delete").click(function () {
            if (!confirm("是否删除？？？"))
                return;
            var row = $("#tb1").bootstrapTable('getSelections');
            //console.log(row);

            if (row.length == 0) {
                alert("请至少选择一条数据");
                return;
            }
            else {
                var ids = new Array();// 声明一个数组
                $(row).each(function () {// 通过获得别选中的来进行遍历
                    ids.push(this.Id);// cid为获得到的整条数据中的一列

                });
                console.log(ids)
                $.ajax({
                    url: "/lanlord/RDel",
                    data: { 'ids': ids },
                    method: "post",
                    success: function (res) {

                        if (res == "Y") {
                            alert("成功");
                            QueryData();
                        }
                        else {
                            alert("失败");
                        }

                    }
                })
            }

        });

        //修改
        $("#btn_edit").click(function () {
            //获取选中的行
            var row = $("#tb1").bootstrapTable('getSelections');
            if (row.length == 0) {
                layer.msg("请先选择要修改的数据");
            } else if (row.length > 1) {
                layer.msg("只能选择一条数据进行操作");
                return;
            }

            $("#myModal").modal('show');
            $("#Name").val(row[0].Name);

            $("#BirthDay").val(row[0].BirthDay111);
            $("#NativePlace").val(row[0].NativePlace);
            $("#Address").val(row[0].Address);
            $("#Tel").val(row[0].Tel);
            $("#cardId").val(row[0].cardId);
            $("#Password").val(row[0].Password);
            $("#Email").val(row[0].Email);
            $("#RoleId").val(row[0].RoleId);
            $("#id").val(row[0].Id);
            $("#txtid").val("1");

        });

        //查询
        function QueryData() {
            $('#tb1').bootstrapTable('refresh', {
                query: {
                    name: $("#txtName").val()
                }

            });
        }

        //数据展示
        function ShowTable() {
            $('#tb1').bootstrapTable({
                url: '/lanlord/Selectlanlord',         //请求后台的URL（*）
                method: 'get',                      //请求方式（*）
                toolbar: '#toolbar',                //工具按钮用哪个容器
                striped: true,                      //是否显示行间隔色
                cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
                pagination: true,                   //是否显示分页（*）
                sortable: true,                     //是否启用排序
                sortOrder: "asc",                   //排序方式
                queryParams: function (params) {
                    params.name = $("#txtName").val();
                    return params;
                },
                sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
                pageNumber: 1,                       //初始化加载第一页，默认第一页
                pageSize: 2,                       //每页的记录行数（*）
                pageList: [2, 25, 50, 100],        //可供选择的每页的行数（*）
                search: false,                       //是否显示表格搜索，此搜索是客户端搜索，不会进服务端，所以，个人感觉意义不大
                strictSearch: true,
                showColumns: true,                  //是否显示所有的列
                showRefresh: false,                  //是否显示刷新按钮
                minimumCountColumns: 1,             //最少允许的列数
                clickToSelect: true,                //是否启用点击选中行
                //height: 500,                        //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
                uniqueId: "Id",                     //每一行的唯一标识，一般为主键列
                showToggle: false,                    //是否显示详细视图和列表视图的切换按钮
                cardView: false,                    //是否显示详细视图
                detailView: true,                   //是否显示父子表
                columns: [{
                    checkbox: true
                }, {
                    field: 'Id',
                    sortable: true,  //排序开启
                    title: '编号'
                }, {
                    field: 'Name',
                    sortable: true,  //排序开启
                    title: '客户姓名'
                }, {
                    field: 'Sex',
                    title: '客户性别',
                    formatter: function (value, row, index) {
                        if (value == 1) {
                            return "男";
                        } else if (value == 2) {
                            return "女";
                        }
                    }
                },
                {
                    field: 'BirthDay',
                    title: '出生日期',
                    formatter: function (value, row, index) {
                        return changeDateFormat(value);
                    }
                },
                {
                    field: 'NativePlace',
                    title: '籍贯'
                },
                {
                    field: 'Address',
                    title: '现居住地'
                },
                {
                    field: 'cardId',
                    title: '身份证号'
                },
                {
                    field: 'Tel',
                    title: '联系电话'
                },
                {
                    field: 'Password',
                    title: '密码'
                },
                {
                    field: 'Email',
                    title: '电子邮箱'
                },
                {
                    field: 'Remark',
                    title: '备注'
                }],
                onExpandRow: function (index, row, $detail) {
                    showSubTable(index, row, $detail);
                }
            });
        }

        function changeDateFormat(cellval) {
            var dateVal = cellval + "";
            if (cellval != null) {
                var date = new Date(parseInt(dateVal.replace("/Date(", "").replace(")/", ""), 10));
                var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
                var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();

                return date.getFullYear() + "-" + month + "-" + currentDate + " ";
            }
        }


    </script>
}

